﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Spreadsheet;

namespace Contacts
{
    public class CustomStylesheet : Stylesheet
    {
        public CustomStylesheet()
        {
            
            
            
            
            
            
            
            //font index 0
            Fonts fonts = new Fonts();
            DocumentFormat.OpenXml.Spreadsheet.Font font = new DocumentFormat.OpenXml.Spreadsheet.Font();
            FontName fontName = new FontName { Val = StringValue.FromString("Tahoma") };
            FontSize fontSize = new FontSize { Val = DoubleValue.FromDouble(8) };
            font.FontName = fontName;
            font.FontSize = fontSize;
            fonts.Append(font);
            
            
            //font index 1
            font = new DocumentFormat.OpenXml.Spreadsheet.Font();
            fontName = new FontName { Val = StringValue.FromString("Tahoma") };
            fontSize = new FontSize { Val = DoubleValue.FromDouble(8) };
            font.FontName = fontName;
            font.FontSize = fontSize;
            font.Bold = new Bold();
            fonts.Append(font);
            fonts.Count = (uint)fonts.ChildElements.Count;

            //fill index 0
            Fills fills = new Fills();
            Fill fill = new Fill();
            PatternFill patternFill = new PatternFill();
            patternFill.PatternType = PatternValues.None;
            fill.PatternFill = patternFill;
            fills.Append(fill);            

            //fill index 1
            fills = new Fills();
            fill = new Fill();
            patternFill = new PatternFill();
            patternFill.PatternType = PatternValues.Gray125;
            fill.PatternFill = patternFill;
            fills.Append(fill);
            fills.Count = (uint)fills.ChildElements.Count;

            //border index 0
            Borders borders = new Borders();
            Border border = new Border();
            border.LeftBorder = new LeftBorder();
            border.RightBorder = new RightBorder();
            border.TopBorder = new TopBorder();
            border.BottomBorder = new BottomBorder();
            border.DiagonalBorder = new DiagonalBorder();
            borders.Append(border);
            
            //border index 1
            border = new Border            
            {                
                LeftBorder = new LeftBorder { Style = BorderStyleValues.Thin },
                RightBorder = new RightBorder { Style = BorderStyleValues.Thin },
                TopBorder = new TopBorder { Style = BorderStyleValues.Thin },
                BottomBorder = new BottomBorder { Style = BorderStyleValues.Thin },
                DiagonalBorder = new DiagonalBorder()
            };
            borders.Append(border);

            //border index 2
            border = new Border
            {
                LeftBorder = new LeftBorder { Style = BorderStyleValues.Medium },
                RightBorder = new RightBorder { Style = BorderStyleValues.Thin },
                TopBorder = new TopBorder { Style = BorderStyleValues.Medium },
                BottomBorder = new BottomBorder { Style = BorderStyleValues.Medium },
                DiagonalBorder = new DiagonalBorder()
            };
            borders.Append(border);

            //border index 3
            border = new Border
            {
                LeftBorder = new LeftBorder { Style = BorderStyleValues.Thin },
                RightBorder = new RightBorder { Style = BorderStyleValues.Thin },
                TopBorder = new TopBorder { Style = BorderStyleValues.Medium },
                BottomBorder = new BottomBorder { Style = BorderStyleValues.Medium },
                DiagonalBorder = new DiagonalBorder()
            };
            borders.Append(border);

            //border index 4
            border = new Border
            {
                LeftBorder = new LeftBorder { Style = BorderStyleValues.Thin },
                RightBorder = new RightBorder { Style = BorderStyleValues.Medium },
                TopBorder = new TopBorder { Style = BorderStyleValues.Medium },
                BottomBorder = new BottomBorder { Style = BorderStyleValues.Medium },
                DiagonalBorder = new DiagonalBorder()
            };

            borders.Append(border);
            borders.Count = ((uint)borders.ChildElements.Count);

            //DocumentFormat.OpenXml.Spreadsheet.CellStyleFormats cellstyleFormats = new DocumentFormat.OpenXml.Spreadsheet.CellStyleFormats();

            //CellFormat cellFormat = new CellFormat();
            //cellFormat.NumberFormatId = 0;
            //cellFormat.FontId = 0;
            //cellFormat.FillId = 0;
            //cellFormat.BorderId = 0;
            //cellstyleFormats.Append(cellFormat);
            //cellstyleFormats.Count = (uint)cellstyleFormats.ChildElements.Count;            
            //cellstyleFormats.Count = 0;
                        
            uint iExcelIndex = 164;
            NumberingFormats numberingFormats = new DocumentFormat.OpenXml.Spreadsheet.NumberingFormats();
            numberingFormats.Count = 0;
            this.Append(numberingFormats);

            CellFormats cellFormats = new DocumentFormat.OpenXml.Spreadsheet.CellFormats();
            //index 0
            CellFormat cellFormat = new CellFormat();
            cellFormat.NumberFormatId = 0;
            cellFormat.FontId = 0;
            cellFormat.FillId = 0;
            cellFormat.BorderId = 0;
            cellFormat.FormatId = 0;
            cellFormats.Append(cellFormat);

            //NumberingFormat numberformat = new NumberingFormat();
            //numberformat.NumberFormatId = iExcelIndex++;
            //numberformat.FormatCode = "dd/mm/yyyy hh:mm:ss";
            //numberingFormats.Append(numberformat);
            //cellFormat = new CellFormat();
            //cellFormat.NumberFormatId = numberformat.NumberFormatId;
            //cellFormat.FontId = 0;
            //cellFormat.FillId = 0;
            //cellFormat.BorderId = 0;
            //cellFormat.FormatId = 0;
            //cellFormat.ApplyNumberFormat = true;
            //cellFormats.Append(cellFormat);

            //numberformat = new NumberingFormat();
            //numberformat.NumberFormatId = iExcelIndex++;
            //numberformat.FormatCode = "@";
            //numberingFormats.Append(numberformat);
            //cellFormat = new CellFormat();
            //cellFormat.NumberFormatId = numberformat.NumberFormatId;
            //cellFormat.FontId = 0;
            //cellFormat.FillId = 0;
            //cellFormat.BorderId = 0;
            //cellFormat.FormatId = 0;
            //cellFormat.ApplyNumberFormat = true;
            //cellFormats.Append(cellFormat);
                      
            //numberingFormats.Count = (uint)numberingFormats.ChildElements.Count;




            //index 0


            //index 1
            cellFormat = new CellFormat
            {
                FontId = 0,
                FillId = 0,
                BorderId = 1,
            };
            cellFormats.Append(cellFormat);

            //index 2
            cellFormat = new CellFormat
            {
                FontId = 1,
                FillId = 1,
                BorderId = 2,
            };
            cellFormats.Append(cellFormat);
            cellFormats.Count = UInt32Value.FromUInt32((uint)cellFormats.ChildElements.Count);
            
            this.Append(fonts);
            this.Append(fills);
            this.Append(borders);
            //this.Append(cellstyleFormats);
            this.Append(cellFormats);
            //this.Append(numberingFormats);

            //CellStyles cellStyles = new DocumentFormat.OpenXml.Spreadsheet.CellStyles();
            //CellStyle cellStyle = new DocumentFormat.OpenXml.Spreadsheet.CellStyle();
            //cellStyle.Name = StringValue.FromString("Normal");
            //cellStyle.FormatId = 0;
            //cellStyle.BuiltinId = 0;
            //cellStyles.Append(cellStyle);
            //cellStyles.Count = UInt32Value.FromUInt32((uint)cellStyles.ChildElements.Count);
            //this.Append(cellStyles);            

            DifferentialFormats dfs = new DifferentialFormats();
            dfs.Count = 0;
            this.Append(dfs);

            TableStyles tss = new TableStyles();
            tss.Count = 0;
            tss.DefaultTableStyle = "TableStyleMedium9";
            tss.DefaultPivotStyle = "PivotStyleLight16";
            this.Append(tss);
        }
    }
}
